import 'package:citiesocial/_include.dart';

class ThemeList extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _ThemeListState();
}

class _ThemeListState extends BaseState<ThemeList> {
  final _list = List<ThemeEntity>();
  var _page = 1;
  var _loadable = true;
  void _loadData() {
    HttpManager.themes(context: context, page: _page).then((list) {
      if (_page <= 1) {
        _list.clear();
      }
      if (list != null && list.length > 0) {
        _loadable = list.length >= HttpManager.default_size;
        _list.addAll(list);
      } else {
        _loadable = false;
      }
      setState(() {});
    });
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.normal(context, '主题'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: DListView(
            itemCount: _list.length,
            itemBuilder: (_, index) => ThemeGoods([_list[index]], true),
            onRefresh: () async {
              _loadable = true;
              _page = 1;
              _loadData();
            },
            onLoadmore: () {
              if (_loadable) {
                _page++;
                _loadData();
              }
            },
          ),
        ),
      );
}
